package org.lintcode_Close.Stage1_Easy_50.O4字符串查找;

/**
 * @Auther: qingle
 * @Date: 2024/8/18-19:22
 * @Description:
 * @version: 1.0
 */
public class 首字母大写 {

	public String capitalizesFirst(String s) {
		// Write your code here
		String[] res = s.split(" ");
		StringBuilder sb = new StringBuilder();
		for(String c: res) {
			sb.append(upper(c));
		}
		return sb.toString().trim();

	}

	public String upper(String s) {
		return (Character.toUpperCase(s.charAt(0))) + s.substring(1) + " ";
	}

	public String capitalizesFirst2(String s) {
		// Write your code here
//		String[] res = s.split(" ");
		StringBuilder sb = new StringBuilder();
		sb.append((Character.toUpperCase(s.charAt(0))));
		for(int i = 1; i < s.length()-1; i++) {
			if (s.charAt(i-1) == ' ' && s.charAt(i) != ' ') {
				sb.append(Character.toUpperCase(s.charAt(i)));
			}
			else{
				sb.append(s.charAt(i));
			}
		}
		sb.append(s.charAt(s.length()-1));
		return sb.toString();

	}

	public static void main(String[] args) {
		首字母大写 a = new 首字母大写();
		System.out.println(a.capitalizesFirst("hello world"));
		System.out.println(a.capitalizesFirst("hello world"));
		System.out.println(a.capitalizesFirst2("hello world "));
	}
}
